﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;
using FilStartUps.FILAPI;

namespace FilStartUps.Controls
{
    public partial class FilStartUps_ForumReply : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {

                    if (Request.QueryString["t"].Trim() != null)
                        ViewState["Skip"] = 0;
                    { GetForumReply(Convert.ToInt32(Request.QueryString["t"].Trim())); }
                }
                catch
                {

                }
            }
        }

        private void GetForumReply(int forumthreadidFK)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {

                try
                {
                    var forumreply = db_connect.ForumReplies.Where(p => p.forumthreadidFK.Equals(forumthreadidFK)
                                    && p.forumreplystatus.Equals(true)).ToList();
                    
                    if (forumreply.Count() < dgforumreply.PageSize)
                    {
                        dgforumreply.PagerStyle.Visible = false;
                    }
                    dgforumreply.DataSource = forumreply;
                    dgforumreply.DataBind();
                    //var forumreply = db_connect.ForumReplies.Where(p => p.forumthreadidFK.Equals(forumthreadidFK)).ToList();
                    //ViewState["ForumReply"] = forumreply;
                    //BindDataList(Convert.ToInt32(ViewState["Skip"]), forumthreadidFK);
                    //dgforumreply.Visible = true;

                    //if (forumreply.Count > 5)
                    //{
                    //    lnkbNext.Visible = true;
                    //    lnkbPrev.Visible = true;
                    //    lnkbPrev.Enabled = false;
                    //}
                    //else
                    //{
                    //    lnkbNext.Visible = false;
                    //    lnkbPrev.Visible = false;
                    //}

                }
                catch (Exception)
                {
                    // dlforumreply.DataSource = forumreply;
                    // dlforumreply.DataBind();
                }

            }

        }

        protected void dgforumreply_Paged(Object sender, DataGridPageChangedEventArgs e)
        {
            dgforumreply.CurrentPageIndex = e.NewPageIndex;
            GetForumReply(Convert.ToInt32(Request.QueryString["t"].Trim()));
           
        }

        //Pagination
        //, int catid
        private void BindDataList(int Skip, int forumid)
        {
            int iPage = Skip + 10;
            int currPage = iPage / 10;
            double TotalPage;

            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var forumreply = db_connect.ForumReplies.Where(p => p.forumthreadidFK.Equals(forumid)).ToList();


                TotalPage = Math.Ceiling((double)forumreply.Count / 10);

                var forumreply_ = forumreply.Skip(Skip).Take(10);

                //if (iPage > forumreply.Count())
                //{
                //    lnkbNext.Enabled = false;
                //}
                //else
                //{
                //    lnkbNext.Enabled = true;
                //}

                dgforumreply.DataSource = forumreply_;
                dgforumreply.DataBind();

                //litPageNumber.Text = "Page " + currPage.ToString().Trim() + " of " + (int)TotalPage;
            }
        }
        //protected void lnkbNext_Click(object sender, EventArgs e)
        //{
        //    //lnkbPrev.Enabled = true;
        //    ViewState["Skip"] = Convert.ToInt32(ViewState["Skip"]) + 10;
        //    BindDataList(Convert.ToInt32(ViewState["Skip"]), Convert.ToInt32(Request.QueryString["t"].Trim()));
        //}

        //protected void lnkbPrev_Click(object sender, EventArgs e)
        //{
        //    ViewState["Skip"] = Convert.ToInt32(ViewState["Skip"]) - 10;
        //    BindDataList(Convert.ToInt32(ViewState["Skip"]), Convert.ToInt32(Request.QueryString["t"].Trim()));
        //    if (Convert.ToInt32(ViewState["Skip"]) == 0)
        //    {
        //      //  lnkbPrev.Enabled = false;
        //    }
        //}
        protected void dgforumreply_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "SelectRow")
            {
                Label lbforumreplyid = (Label)dgforumreply.Items[e.Item.ItemIndex].FindControl("replyid");
                Response.Redirect("~/Report.aspx?f=" + lbforumreplyid.Text + "&t=" + Request.QueryString["t"].Trim());
            }
        }
    }
}